package com.cswnode.demo.service.impl;

import com.cswnode.demo.entity.Post;
import com.cswnode.demo.entity.Resume;
import com.cswnode.demo.dao.ResumeDao;
import com.cswnode.demo.enums.YesOrNoEnum;
import com.cswnode.demo.service.PostService;
import com.cswnode.demo.service.ResumeService;
import com.cswnode.demo.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 简历(Resume)表服务实现类
 *
 * @author makejava
 * @since 2021-03-26 16:05:34
 */
@Service("resumeService")
public class ResumeServiceImpl implements ResumeService {
    @Resource
    private ResumeDao resumeDao;
    @Autowired
    private PostService postService;

    /**
     * 通过ID查询单条数据
     *
     * @param resumeId 主键
     * @return 实例对象
     */
    @Override
    public Resume queryById(Long resumeId) {
        return this.resumeDao.queryById(resumeId);
    }

    @Override
    public Resume queryById() {
        Long userId = Utils.getLoginUser().getUserId();
        Resume resume = resumeDao.queryByUid(userId);
        if (resume == null){
            Resume resume1 = new Resume();
            resume1.setIsDelete(YesOrNoEnum.NO.getCode());
            resume1.setFromId(userId);
            resumeDao.insert(resume1);
            return resume1;
        }
        return resume;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Resume> queryAllByLimit(int offset, int limit) {
        return this.resumeDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param resume 实例对象
     * @return 实例对象
     */
    @Override
    public Resume insert(Resume resume) {
        this.resumeDao.insert(resume);
        return resume;
    }

    @Override
    public String sendResume(Long postId) {
        Post post = postService.queryById(postId);
        Long userId = Utils.getLoginUser().getUserId();
        Resume resume = resumeDao.queryByUid(userId);
        Long resumeId1 = resume.getResumeId();
        String id = String.valueOf(resumeId1);
        if (null == post){
            return "发送失败!";
        }
        String resumeId = post.getResumeId();
        if (StringUtils.isEmpty(resumeId)){
            post.setResumeId(id);
            postService.update(post);
            return null;
        }
        String[] split = resumeId.split(",");
        for (String s : split){
            if (id.equals(s)){
                return "简历已投过！无需在投！";
            }
        }
        StringBuffer sb = new StringBuffer();
        sb.append(resumeId);
        sb.append(",");
        sb.append(id);
        post.setResumeId(sb.toString());
        postService.update(post);
        return null;
    }

    @Override
    public List<Resume> getResume(Long postId) {
        Post post = postService.queryById(postId);
        if (null == post){
            return null;
        }
        String resumeId = post.getResumeId();
        if (StringUtils.isEmpty(resumeId)){
            return null;
        }
        String[] split = resumeId.split(",");
        List<Resume> resumes = resumeDao.queryBath(split);
        return resumes;
    }

    /**
     * 修改数据
     *
     * @param resume 实例对象
     * @return 实例对象
     */
    @Override
    public Resume update(Resume resume) {
        resume.setFromId(null);
        this.resumeDao.update(resume);
        return this.queryById(resume.getResumeId());
    }

    /**
     * 通过主键删除数据
     *
     * @param resumeId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long resumeId) {
        return this.resumeDao.deleteById(resumeId) > 0;
    }
}